<?php
/**
 * Created by PhpStorm.
 * Date: 2016/11/16
 * Time: 下午2:05
 */
namespace app\api\controller;

use app\api\model\UserException;
use app\common\model\Alipay;
use app\common\model\Axb;
use app\common\model\MerchLevelOrder;
use app\common\model\Order;
use app\common\model\UserDistributorOrder;
use app\common\model\UserLevelOrder;
use app\common\model\Wechatpay;
use think\Exception;
use think\facade\Db;

class CallNotify extends Notify
{

    public function index()
    {
        try{
            Db::startTrans();

            $json_str = file_get_contents('php://input');//获取返回来的通知
            if($json_str=='')
            {
                $return = ['code'=>'0','msg'=>'空数据'];
            }else{
                file_put_contents(__DIR__.'/pay_notify/call.txt',$json_str."\r\n",FILE_APPEND);

                $jsonArr = json_decode($json_str,true);

                foreach ($jsonArr as $json){
                    if(!$json || !isset($json['out_id'])){
                        $return = ['code'=>'0','msg'=>'数据格式有误'];
                    }else{
                        //接收通话记录
                        switch ($json['out_id']){
                            case Axb::axbTag()://axb通话
                                Axb::callLog($json);
                                break;
                            default:
//                                throw new UserException('无通话标记');
                        }

                        $return = ['code'=>'0','msg'=>'成功'];
                    }
                }

                echo '{ "code": 0, "msg": "成功"}';//阿里云有病 不这样不让保存回调地址


            }
            Db::commit();

        }catch (\Exception $exception){
            Db::rollback();
            $return = ['code'=>1,'msg'=>$exception->getMessage()];
            echo json_encode($return,JSON_UNESCAPED_UNICODE);
        }

    }


}
